package com.zrrd.gmall.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.gmall.model.enums.PaymentStatus;
import com.zrrd.gmall.model.order.OrderInfo;
import com.zrrd.gmall.model.payment.PaymentInfo;
import com.zrrd.gmall.payment.mapper.PaymentInfoMapper;
import com.zrrd.gmall.payment.service.PaymentService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo>implements PaymentService {
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //先查
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderInfo.getId());
        wrapper.eq("payment_type",paymentType);
        int count = this.count(wrapper);
        //如果存在不操作
        if (count >0){
            return;
        }
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        //执行
        this.save(paymentInfo);

    }
}
